﻿using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using GSQ.BusinessEntities;
using GSQ.DataAccess.DataSetTableAdapters;

namespace GSQ.DataAccess
{
    public class SuplidoresDA
    {
        private SUPLIDORESTableAdapter Adapter = new SUPLIDORESTableAdapter();
        private V_SUPLIDORESTableAdapter vAdapter = new V_SUPLIDORESTableAdapter();
        private DataSet.SUPLIDORESDataTable entities;
        private DataSet.SUPLIDORESRow entity;

        public decimal Insert(SUPLIDORES value)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    Adapter.Connection = cnn;

                    entities = new DataSet.SUPLIDORESDataTable();

                    entity = entities.NewSUPLIDORESRow();

                    entity.ID = value.ID;
                    entity.SUPLIDOR = value.SUPLIDOR;
                    entity.VENDEDOR = value.VENDEDOR;
                    entity.RNC = value.RNC;
                    entity.DIRECCION = value.DIRECCION;
                    entity.ID_CIUDAD = value.ID_CIUDAD;
                    entity.TELEFONO1 = value.TELEFONO1;
                    if (value.TELEFONO2 != null)
                        entity.TELEFONO2 = value.TELEFONO2;
                    if (value.FAX != null)
                        entity.FAX = value.FAX;
                    if (value.OTRO != null)
                        entity.OTRO = value.OTRO;
                    if (value.EMAIL != null)
                        entity.EMAIL = value.EMAIL;
                    if (value.WEB != null)
                        entity.WEB = value.WEB;
                    if (value.OBSERVACIONES != null)
                        entity.OBSERVACIONES = value.OBSERVACIONES;
                    if (value.CUENTA != null)
                        entity.CUENTA = value.CUENTA;
                    entity.ID_BANCO = value.ID_BANCO;
                    entity.ID_MONEDA = value.ID_MONEDA;
                    entity.PLAZO_PAGO = value.PLAZO_PAGO;
                    entity.ACTIVO = value.ACTIVO;

                    this.entities.AddSUPLIDORESRow(this.entity);

                    this.Adapter.Update(entities);

                    value.ID = entity.ID;
                    return value.ID;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public decimal Update(SUPLIDORES value)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    entities = Adapter.GetDataByID(value.ID);
                    entity = entities.FirstOrDefault();

                    entity.SUPLIDOR = value.SUPLIDOR;
                    entity.VENDEDOR = value.VENDEDOR;
                    entity.RNC = value.RNC;
                    entity.DIRECCION = value.DIRECCION;
                    entity.ID_CIUDAD = value.ID_CIUDAD;
                    entity.TELEFONO1 = value.TELEFONO1;
                    if (value.TELEFONO2 != null)
                        entity.TELEFONO2 = value.TELEFONO2;
                    if (value.FAX != null)
                        entity.FAX = value.FAX;
                    if (value.OTRO != null)
                        entity.OTRO = value.OTRO;
                    if (value.EMAIL != null)
                        entity.EMAIL = value.EMAIL;
                    if (value.WEB != null)
                        entity.WEB = value.WEB;
                    if (value.OBSERVACIONES != null)
                        entity.OBSERVACIONES = value.OBSERVACIONES;
                    if (value.CUENTA != null)
                        entity.CUENTA = value.CUENTA;
                    entity.ID_BANCO = value.ID_BANCO;
                    entity.ID_MONEDA = value.ID_MONEDA;
                    entity.PLAZO_PAGO = value.PLAZO_PAGO;
                    entity.ACTIVO = value.ACTIVO;
                    this.Adapter.Update(entities);

                    return entity.ID;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public SUPLIDORES GetById(decimal id)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.vAdapter.Connection = cnn;

                    SUPLIDORES suplidor;

                    DataSet.V_SUPLIDORESRow rw = this.vAdapter.GetDataByID(id).FirstOrDefault();

                    if (rw != null)
                    {
                        suplidor = GetEntity(rw);
                    }
                    else
                        suplidor = null;

                    return suplidor;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public List<SUPLIDORES> GetAll()
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.vAdapter.Connection = cnn;

                    List<SUPLIDORES> lst = new List<SUPLIDORES>();
                    foreach (DataSet.V_SUPLIDORESRow rw in this.vAdapter.GetData())
                    {
                        lst.Add(GetEntity(rw));
                    }
                    cnn.Close();
                    return lst;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public List<SUPLIDORES> GetBySuplidor(string value)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.vAdapter.Connection = cnn;

                    List<SUPLIDORES> lst = new List<SUPLIDORES>();
                    foreach (DataSet.V_SUPLIDORESRow rw in this.vAdapter.GetDataBySuplidor(value.Trim()))
                    {
                        lst.Add(GetEntity(rw));
                    }
                    cnn.Close();
                    return lst;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public SUPLIDORES GetByRnc(string value)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.vAdapter.Connection = cnn;

                    SUPLIDORES suplidor;

                    DataSet.V_SUPLIDORESRow rw = this.vAdapter.GetDataByRnc(value.Trim()).FirstOrDefault();

                    if (rw != null)
                    {
                        suplidor = GetEntity(rw);
                    }
                    else
                        suplidor = null;

                    return suplidor;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public List<SUPLIDORES> GetByCiudad(string value)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.vAdapter.Connection = cnn;

                    List<SUPLIDORES> lst = new List<SUPLIDORES>();
                    foreach (DataSet.V_SUPLIDORESRow rw in this.vAdapter.GetDataByCiudad(value.Trim()))
                    {
                        lst.Add(GetEntity(rw));
                    }
                    cnn.Close();
                    return lst;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public List<SUPLIDORES> GetByIdCiudad(decimal value)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.vAdapter.Connection = cnn;

                    List<SUPLIDORES> lst = new List<SUPLIDORES>();
                    foreach (DataSet.V_SUPLIDORESRow rw in this.vAdapter.GetDataByIdCiudad(value))
                    {
                        lst.Add(GetEntity(rw));
                    }
                    cnn.Close();
                    return lst;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        private SUPLIDORES GetEntity(DataSet.V_SUPLIDORESRow rw)
        {
            SUPLIDORES suplidor = null;
            if (rw != null)
            {
                suplidor = new SUPLIDORES();

                suplidor.ID = rw.ID;
                suplidor.SUPLIDOR = rw.SUPLIDOR;
                suplidor.VENDEDOR = rw.VENDEDOR;
                suplidor.RNC = rw.RNC;
                suplidor.DIRECCION = rw.DIRECCION;
                suplidor.ID_CIUDAD = rw.ID_CIUDAD;
                suplidor.CIUDADES = new CIUDADES { ID = rw.ID_CIUDAD, CIUDAD = rw.CIUDAD };
                suplidor.TELEFONO1 = rw.TELEFONO1;
                if (!rw.IsTELEFONO2Null())
                    suplidor.TELEFONO2 = rw.TELEFONO2;
                if (!rw.IsFAXNull())
                    suplidor.FAX = rw.FAX;
                if (!rw.IsOTRONull())
                    suplidor.OTRO = rw.OTRO;
                if (!rw.IsEMAILNull())
                    suplidor.EMAIL = rw.EMAIL;
                if (!rw.IsWEBNull())
                    suplidor.WEB = rw.WEB;
                if (!rw.IsOBSERVACIONESNull())
                    suplidor.OBSERVACIONES = rw.OBSERVACIONES;
                if (!rw.IsCUENTANull())
                    suplidor.CUENTA = rw.CUENTA;
                suplidor.ID_BANCO = rw.ID_BANCO;
                suplidor.BANCOS = new BANCOS { ID = rw.ID_BANCO, BANCO = rw.BANCO };
                suplidor.ID_MONEDA = rw.ID_MONEDA;
                suplidor.MONEDAS = new MONEDAS { ID = rw.ID_MONEDA, MONEDA = rw.MONEDA };
                suplidor.PLAZO_PAGO = rw.PLAZO_PAGO;
                suplidor.ACTIVO = rw.ACTIVO;
                suplidor.MONTO_ADEUDADO = rw.MONTO_ADEUDADO;
            }

            return suplidor;
        }
    }
}